package com.hong.config;

import com.hong.service.PasswordDecodable;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Created by John on 2018/10/28.
 */
public class PasswordDecodePostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object o, String s) throws BeansException {
        if (o instanceof PasswordDecodable) {
            String encodedPassword = ((PasswordDecodable) o).getEncodedPassword();
            String decodedPassword = decodePassword(encodedPassword);
            ((PasswordDecodable) o).setDecodedPassword(decodedPassword);
        }
        return o;
    }

    private String decodePassword(String encodedPassword){
        //解密密码
        String decodedPassword = encodedPassword.replace("#*123","").trim();
        return decodedPassword;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String s) throws BeansException {
        return o;
    }
}
